import React,{Component} from 'react';
import {Link} from 'react-router'
import Pages from '../Pages'
import { Spin,message} from 'antd';
import * as pub from '../../config/public';
import * as fetch from '../../config/fetch';
import Layer from '../Layer';
class NewsCenter extends Component {
	constructor(args) {
		super()
		this.state = {
			MessgeList : [],
			activeIndex : 0,
			totalPage:1,
			loading:false,
			elecNewsCenter:0,
			pageno:1,
			visibleDH:false
		}
			
	}

	componentWillMount() {
		this.unreadinformationlist();
	}
	unreadinformationlist(){
		this.setState({
			loading: true
		})
		fetch.unreadList({pageno:this.state.pageno,companyCode:JSON.parse(localStorage.getItem('user')).manageCom}).then((data) => {
			if (data.result === 'RC100') {
			this.setState({
				MessgeList: data.informationList,
				totalPage: data.total
			}, () => {
				this.setState({
					loading: false
				})
			})
			}else {
				message.error(data.errMsg, 3);
				this.setState({
					MessgeList:[],
					loading: false,
				})
			}
			
		}, (res) => {
			pub.reject();
			this.setState({
				loading: false
			})
		})	
	}
	readinformationlist(){
		this.setState({
			loading: true
		})
		fetch.readList({pageno:this.state.pageno,companyCode:JSON.parse(localStorage.getItem('user')).manageCom}).then((data) => {
			if (data.result === 'RC100') {
			this.setState({
				MessgeList: data.informationList,
				totalPage: data.total
			}, () => {
				this.setState({
					loading: false
				})
			})
        } else {
				message.error(data.errMsg, 3);
				this.setState({
					MessgeList:[],
					loading: false
				})
			}
		}, (res) => {
			pub.reject();
			this.setState({
				MessgeList:[],
				loading: false
			})
		})	
	}
	jump(page) {
		this.setState({
			pageno:page
		},()=>{
			if(this.state.elecNewsCenter===0){
				this.unreadinformationlist();
			}else{
				this.readinformationlist();
			}
		})
	}
	Details(id){
		fetch.viewmessage(id).then((data)=>{
			if(data.result === 'RC100'){
			}else{
				message.error(data.errMsg, 3);
			}
		}, (res) => {
			pub.reject(res);
		})
	}
	clickMe(item){
		if(item.informationType==='1'&&item.isOutOfDate===0){
            this.setState({
				visibleDH:true,
				url:item.inforURL
		    })  
		}else{
			message.warning('该消息已下架', 3);
		}
	}
	ok(url){
		window.location.href='http://'+url
	}
	render() {

		return (
					<div className="wrapper bg-F0F4F6">
		           <div className="layout clearFix">
				    <div className="row pad-t-20">
						<div className="filter clearFix">
					    <ul className="filter-ul nav clearFix">
						<li><a className={this.state.elecNewsCenter === 0?"active":""} onClick={()=>{this.setState({elecNewsCenter:0,pageno:1},()=>this.unreadinformationlist())}}>未读</a></li>
						<li><a className={this.state.elecNewsCenter === 1?"active":""} onClick={()=>{this.setState({elecNewsCenter:1,pageno:1},()=>this.readinformationlist())}}>已读</a></li>
					</ul>
					</div>
				    <Spin spinning={this.state.loading} tip="加载列表中...">
					  <ul className="msg-gl" style={{minHeight:'539px'}} >
					  {this.state.MessgeList.map((item,index)=>{
					  	// let papaerId = pub.getPaperId(item.informationNote);
					  	return(
						      <li key={index} onClick={()=>this.Details(item.informationId)}>
						      	<p style={{width:'100%',wordWrap: 'break-word'}} className="title">{item.informationTitle !== null?item.informationTitle:'未知标题'}</p>
						      	<div style={{width:'100%',wordWrap: 'break-word'}}>{item.informationNote}</div>
                                {/* <a href={'http://'+item.inforURL}>任务地址</a> */}
                                {
                                	item.informationType === '1' && item.isExam === 0 ?
                                		<a onClick={()=>this.clickMe(item)} style={{cursor:"pointer"}}>任务地址</a>
                                	:null
                                }
								{ 
									item.isExam === 1 && item.informationType === '1'?
										<div>
											<Link to={'NewsCenter/AnswerRecord/'+item.inforURL.split('/')[item.inforURL.split('/').length-1]}><span>答题历史</span></Link>
										</div>
									:null	
								}
										
								{	
									
									item.informationType === '2' && item.isExam === 0 && Number(item.isOtherExam) === 0 ?
										<div>
											<Link to={'NewsCenter/RadioList/'+item.inforURL.split('/')[item.inforURL.split('/').length-1]}><span>随机答题</span></Link>
										</div>
									:null
								}
								{ 
									Number(item.isOtherExam) === 1  && item.informationType === '2'?
										<div>
											<Link to={'NewsCenter/RandomAnswerRecord/'+item.inforURL.split('/')[item.inforURL.split('/').length-1]}><span>随机答题历史</span></Link>
										</div>
									:null	
								}
								{/*<div>
									<Link to={'NewsCenter/RadioList/'+item.inforURL.split('/')[item.inforURL.split('/').length-1]}><span>随机答题</span></Link>
								</div>*/}
						      	<p className="time">{pub.formatTimestamp(item.createTime)}</p>
						      	{item.operationState !== '1'?
						      	<i className="icon-envelope-alt"></i>
						      	:
						      	<i className="icon-envelope" title="删除"></i>
						      }
						      	
						      	{/*<i className="icon-remove" title="删除"></i>*/}
						      </li>
					  		)
					  })}
					  </ul>
					  </Spin>
					  <Layer 
				            show={this.state.visibleDH} 
				            hide={(e)=>{this.setState({visibleDH:false})}}
				            width="400px"
				            height="270px"
			                >
				            	<div className="row">
				            			<div className="pop-box-1"  style={{textAlign:'left'}}>																					
				            				<p style={{textAlign:'center'}}>是否测试开始</p>																				
				            			</div>
				            			<div className="pop-btn">
				            				<a onClick={()=>{this.setState({visibleDH:false})}} className="btn btn-blue"  style={{borderRadius: '5px',marginRight:'50px'}}>否</a>
				            				<a className="btn btn-blue" style={{borderRadius: '5px'}} onClick={()=>this.ok(this.state.url)}>是
				            				</a>
				            			</div>
				            		</div>
			                </Layer>
					  <div className="line"></div>
						<Pages totalPage={this.state.totalPage} jump={this.jump.bind(this)} />
					</div>
			</div>
			</div>

		)
	}

}
export default NewsCenter

